package sdfi.lee.DAO;

import java.sql.Connection;
import java.sql.ResultSet;

import org.hibernate.Session;

import sdfi.lee.Entity.News;
import sdfi.lee.Entity.Notice;
import sdfi.lee.Properties.ReadXMLFromString;

public class FileDAO {
	public Notice getFileByFilepath(String path)
	{
		Session session=DAO.openSession();
		Connection con=session.connection();
		try
		{
		con.setAutoCommit(true);
		String sql="select file from file where path='"+path+"'";
		
		ResultSet rs=con.createStatement().executeQuery(sql);
		if(rs.next())
		{
			Notice notice=new ReadXMLFromString("<root>"+rs.getString("file")+"</root>")
			.readerPolicyFromString();
			
			return notice;
		
		}
		else 
		{
			return null;
		}
		
		}
		catch(Exception ee)
		{
			ee.printStackTrace();
			return null;
		}
		finally
		{
			DAO.closeSession();
		}
		
	}
/*获取公告*/
	public Notice getFileNoticeByFile(String path)
	{

		Session session=DAO.openSession();
		Connection con=session.connection();
		try
		{
			con.setAutoCommit(true);
			String sql="select id,file from file where path='"+path+"'";
			
			ResultSet rs=con.createStatement().executeQuery(sql);
			
			if(rs.next())
			{
				
			Notice notice=new ReadXMLFromString("<root>"+rs.getString("file")+"</root>")
			.readerNoticeFromString();
			
			return notice;
		
			}
			else 
			{
				
			return null;
			}
		
		}
		catch(Exception ee)
		{
				ee.printStackTrace();
				return null;
		}
		finally
		{
				DAO.closeSession();
		}
	}
	
	
	public Notice getPolicyByFilepath(String path)
	{
		
		Notice notice=null;
		Session session=DAO.openSession();
		Connection con=session.connection();
		try
		{
			con.setAutoCommit(true);
			String sql="select file from file where path='"+path+"'";
			long lt=System.currentTimeMillis();
			ResultSet rs=con.createStatement().executeQuery(sql);
		
			if(rs.next())
			{
			ReadXMLFromString rxf=new ReadXMLFromString();
			rxf.setSource("<root>"+rs.getString("file")+"</root>");
			notice=new Notice();
			notice.setDate(rxf.readerValueByNode("/root/modifytime"));
			notice.setNoticetitle(rxf.readerValueByNode("/root/title"));
			notice.setNoticecontent(rxf.readerValueByNode("/root/body"));
		
			return notice;
			}
			else 
			{
				
			return null;
			}
		
		}
		catch(Exception ee)
		{
				ee.printStackTrace();
				return null;
		}
		finally
		{
				DAO.closeSession();
		}
		
	}

	
	public Notice getNewsByFilepath(String path)
	{
		
		
		Session session=DAO.openSession();
		Connection con=session.connection();
		try
		{
			con.setAutoCommit(true);
			String sql="select file from file where path='"+path+"'";
			
			ResultSet rs=con.createStatement().executeQuery(sql);
			if(rs.next())
			{
				Notice notice=new ReadXMLFromString("<root>"+rs.getString("file")+"</root>")
				.readerNewsFromString();
				return notice;
			
			}
			else 
			{
				System.out.println("adsfasfd121212");
				return null;
				
			}
			
			}
		catch(Exception ee)
		{
				ee.printStackTrace();
				return null;
		}
		finally
		{
				DAO.closeSession();
		}
		
	}

	public String getRollNoticeByFilepath(String path)
	{
		
		
		Session session=DAO.openSession();
		Connection con=session.connection();
		try
		{
			con.setAutoCommit(true);
//			
			String sql="select file from file where path ='"+path.replaceFirst("//", "/")+"'";
		
			ResultSet rs=con.createStatement().executeQuery(sql);
						
			if(rs.next())
			{
				
				
				String tem= rs.getString("file");
			
				return tem;
			
			}
			else 
			{
				
			return null;
			}
		
		}
		catch(Exception ee)
		{
				ee.printStackTrace();
				return null;
		}
		finally
		{
				DAO.closeSession();
		}
		
	}
}
